package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.EnrollStudentCause;
import com.xmy.cultivate.entity.EnrollStudentSpeech;
import com.xmy.cultivate.service.IEnrollStudentCauseService;
import com.xmy.cultivate.service.IEnrollStudentSpeechService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/enrollStudentCause")
@Scope("prototype")
public class EnrollStudentCauseController extends BaseController {
    @Autowired
    IEnrollStudentCauseService iEnrollStudentCauseService;
    @Autowired
    IEnrollStudentSpeechService iEnrollStudentSpeechService;
    @GetMapping()
    public Result getList(@RequestParam Map<String, String> reMap){
        int pageNum = StringUtils.isEmpty(reMap.get("pageNum")) ? 1 : Integer.parseInt(reMap.get("pageNum"));
        int pageSize = StringUtils.isEmpty(reMap.get("pageSize")) ? 10: Integer.parseInt(reMap.get("pageSize"));
        IPage<EnrollStudentCause> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper<EnrollStudentCause> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        iEnrollStudentCauseService.page(iPage, queryWrapper);
        return new Result(Code.OK,new PageResult<>(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }
    @PostMapping()
    public Result goToSave(@RequestBody EnrollStudentCause enrollStudentCause){
        enrollStudentCause.setDeleted(0);
        iEnrollStudentCauseService.save(enrollStudentCause);
        return new Result(Code.OK,"","");
    }
    @PutMapping()
    public Result goToEdit(@RequestBody EnrollStudentCause enrollStudentCause){
        iEnrollStudentCauseService.updateById(enrollStudentCause);
        return new Result(Code.OK,"","");
    }
    @DeleteMapping()
    public Result goToDel(@RequestParam Map<String, String> reMap){
        String id = reMap.get("id");
        if (id == null || id.isEmpty()) {
            return new Result(Code.ERROR,"","id不能为空！");
        }
        String[] ids = id.split(",");
        for (String nid: ids) {
            long count = iEnrollStudentSpeechService.count(new QueryWrapper<EnrollStudentSpeech>().eq("student_cause_id", nid));
            if (count > 0) {
                EnrollStudentCause byId = iEnrollStudentCauseService.getById(Long.valueOf(nid));
                return new Result(Code.ERROR, "", byId.getName() + " 已被使用，请先删除话术列表！");
            }
        }
        iEnrollStudentCauseService.remove(new QueryWrapper<EnrollStudentCause>().inSql("id", id));
        return new Result(Code.OK,"","");
    }
}
